package net.forsalemall.android;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.Action;
import com.utils.Constants;
import com.utils.HttpUtils;
import com.utils.TextDrawable;
import com.utils.Utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.text.InputFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ContactUs extends Activity {

	private String countryCode;
	private String phonePrefix;
	
	private int phoneDigit;

	private String phoneNo;
	private EditText phoneNoField;
	private EditText nameField;
	private EditText emailFiled;
	private EditText inquiryField;

	protected void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.contact_us);
			final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
			actionBar.setTitle(getString(R.string.title_contact_us));
			countryCode = getIntent()
					.getStringExtra(Constants.KEY_COUNTRY_CODE);
			phoneNo = getIntent().getStringExtra(Constants.KEY_PHONE_NO);
			actionBar.setHomeAction(new Action() {

				public void performAction(View view) {
					backToHome();
				}

				public int getDrawable() {
					// TODO Auto-generated method stub
					return R.drawable.ic_title_home_default;
				}
			});
			actionBar.setDisplayHomeAsUpEnabled(true);

			phoneNoField = (EditText) findViewById(R.id.phone_no_field);
			phonePrefix = Constants.COUNTRY_CODE_KUWAIT.equals(countryCode) ? Constants.PHONE_CODE_KUWAIT
					: Constants.PHONE_CODE_UAE;
			TextDrawable td = new TextDrawable(phonePrefix + "-");
			td.setTextSize(phoneNoField.getTextSize());
			phoneNoField.setCompoundDrawablesWithIntrinsicBounds(td, null,
					null, null);
			phoneDigit = Constants.COUNTRY_CODE_KUWAIT.equals(countryCode) ? Constants.NUMBER_OF_PHONE_DIGIT_KUWAIT
					: Constants.NUMBER_OF_PHONE_DIGIT_UAE;
			InputFilter[] FilterArray = new InputFilter[1];
			FilterArray[0] = new InputFilter.LengthFilter(phoneDigit);
			phoneNoField.setFilters(FilterArray);
			
			if ((phoneNo != null) && (phoneNo.length() > phonePrefix.length())
					&& (phoneNo.startsWith(phonePrefix))) {
				phoneNoField.setText(phoneNo.substring(phonePrefix.length()));
				
			}else{
				phoneNoField.setText("");
			}

			nameField = (EditText) findViewById(R.id.name_field);
			emailFiled = (EditText) findViewById(R.id.email_address_field);
			inquiryField = (EditText) findViewById(R.id.inquiry_field);
			findViewById(R.id.button_sumit).setOnClickListener(
					new View.OnClickListener() {

						public void onClick(View v) {
							submit();
						}
					});
			findViewById(R.id.button_reset).setOnClickListener(
					new View.OnClickListener() {

						public void onClick(View v) {
							reset();
						}
					});

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void backToHome() {
		try {
			startActivity(new Intent(this, HomeActivity.class)
					.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void submit() {
		try {
			String name = nameField.getText().toString();
			String email = emailFiled.getText().toString();
			String inquiry = inquiryField.getText().toString();
			String phoneNo = phoneNoField.getText().toString();
			if ((name.length() == 0) || (!Utils.checkEmail(email))
					|| (inquiry.length() == 0)) {
				Utils.showAlertMessage(this, null,
						getString(R.string.fill_all_info_correctly));
			} else if(phoneDigit!=phoneNo.length()){
				if(phoneDigit==8){
					Utils.showAlertMessage(this, null,
							getString(R.string.number_should_be_8_digit));
				}else{
					Utils.showAlertMessage(this, null,
							getString(R.string.number_should_be_9_digit));
				}	
			} else {
				submit((phonePrefix + (phoneNo)),
						name, email, inquiry);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void submit(final String phNo, final String name,
			final String email, final String inquiry) throws Exception {
		new AsyncTask<String, String, String>() {
			private ProgressDialog progressDialog;
			private String error;

			protected void onPreExecute() {
				try {
					progressDialog = Utils.getProgressDialog(ContactUs.this,
							null, getString(R.string.loading));
					progressDialog.show();
					error = null;
				} catch (Exception e) {
					error = e.getMessage();
					e.printStackTrace();
				}
			}

			protected String doInBackground(String... arg0) {
				try {
					HttpUtils.postInquiry(Utils.getDeviceId(ContactUs.this),
							phNo, name, email, inquiry);

				} catch (Exception e) {
					error = e.getMessage();
					e.printStackTrace();
				}
				return null;
			}

			protected void onPostExecute(String unused) {
				try {
					progressDialog.dismiss();
					if (error == null) {
						reset();
						Utils.showAlertMessage(ContactUs.this, null,
								getString(R.string.inquiry_posted));
					} else {
						Utils.showAlertMessage(ContactUs.this, null, error);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.execute();
	}

	private void reset() {
		try {
			nameField.setText("");
			emailFiled.setText("");
			inquiryField.setText("");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
